package com.situ.saveqq;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText etNumber;
    private EditText etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 给Activity绑定xml中布局
        setContentView(R.layout.activity_main);

        // 找到xml中的控件
        etNumber = findViewById(R.id.et_number);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);

        // 读取文件保存的内容，然后设置到账号和密码的EditText中
        Map<String, String> map = SPSaveQQ.getUserInfo(this);
        etNumber.setText(map.get("number"));
        etPassword.setText(map.get("password"));


        // 给Button绑定点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当用户点击的时候获取用户输入的qq账号和密码
                String number = etNumber.getText().toString();
                String password = etPassword.getText().toString();
                // 检查账号和密码是否正确  卫操作，遇到非法的输入，直接return
                if (TextUtils.isEmpty(number)) {
                    Toast.makeText(MainActivity.this, "请输入QQ账号：", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(MainActivity.this, "请输入QQ密码：", Toast.LENGTH_LONG).show();
                    return;
                }

                // 执行的都是正常的业务代码
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
               boolean isSaveSuccess =  SPSaveQQ.saveUserInfo(MainActivity.this, number, password);
               if (isSaveSuccess) {
                   Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
               } else {
                   Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_LONG).show();
               }
            }
        });
    }
}
